/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jack;

import example.chooser.ColorChooser;
import java.io.IOException;
import java.util.Stack;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.*;

/**
 *
 * @author jack
 */
public class EditForm extends Form implements CommandListener, ItemCommandListener {

    private CounterItem item;
    private ColorChooser colors;
    HomeworkMidlet midlet;
    Command backCommand;
    Command okCommand;
    /*
     * name,count,seq,imagepath
     */
    TextField nameText;
    TextField countText;
    TextField seqText;
    ImageList imageList;
    ColorfulItem foreColorfulItem;
    ColorfulItem bgColorfulItem;
    ColorfulItem countKeyColorfulItem;
    ChoiceGroup anykeyCG;
    ChoiceGroup fullSceenModeCG;
    ChoiceGroup showImageColorCG;
    ChoiceGroup keepScaleCG;
    private Command foreColorCommand = new Command("前景色", Command.SCREEN, 0);
    private Command backgroundColorCommand = new Command("背景色", Command.ITEM, 1);
    private Command okFgCommand = new Command("Ok", Command.SCREEN, 0);
    private Command okBgCommand = new Command("Ok", Command.SCREEN, 0);
    private Command okColorCommand = new Command("Ok", Command.ITEM, 0);
    private Command deleteCountKeyCommand = new Command("清除", Command.SCREEN, 0);

    public EditForm(HomeworkMidlet midlet) {
        super("");

        this.midlet = midlet;

        backCommand = new Command("返回", Command.BACK, 0);
        okCommand = new Command("确定", Command.SCREEN, 0);
        addCommand(backCommand);
        addCommand(okCommand);
        setCommandListener(this);



        //nameItem=new StringItem("", "");
        nameText = new TextField("显示名称", "", 100, TextField.ANY);

        countText = new TextField("初始数量", "0", 100, TextField.NUMERIC);

        seqText = new TextField("显示顺序", "0", 10, TextField.NUMERIC);

        imageList = new ImageList("选择图片", List.POPUP);

        imageList.append("南无阿弥陀佛", "/icons/amtf_icon.jpg");
        imageList.append("南无本师释迦牟尼佛", "/icons/benshishijiamounifo_icon.jpg");
        imageList.append("南无地藏王菩萨", "/icons/dizangpusa_icon.jpg");
        imageList.append("南无地藏王菩萨2", "/icons/dizangwangpusa_icon.jpg");
        imageList.append("南无观世音菩萨-地藏缘", "/icons/guanshiyinpusa_dizangyuan_icon.jpg");
        imageList.append("南无观世音菩萨", "/icons/guanyinpusa_icon.jpg");
        imageList.append("嚂字放光(方)", "/icons/lan_fang_icon.jpg");
        imageList.append("嚂字放光(圆)", "/icons/lan_yuan_icon.jpg");
        imageList.append("嚂字放光(240X320)", "/icons/lan_icon.jpg");


        imageList.append("毗卢观世音菩萨正身像", "/icons/piluguanshiyinzhengshenxiang_icon.jpg");
        imageList.append("千手观音(立)", "/icons/qianshouguanyin1_icon.jpg");
        imageList.append("千手观音(坐)", "/icons/qianshouguanyin2_icon.jpg");
        imageList.append("子归图", "/icons/ziguitu_icon.jpg");
        imageList.append("准提菩萨(敦煌肤脸)", "/icons/zhuntipusa_dunhuang_icon.jpg");
        imageList.append("准提菩萨", "/icons/zhuntipusa_icon.jpg");
        imageList.append("准提菩萨唐卡", "/icons/zhuntipusa_tangka_icon.jpg");

        imageList.append("准提佛母", "/icons/zhuntifomu_icon.jpg");
        imageList.append("心月轮(方)", "/icons/zhuntizhoulun_icon.jpg");
        imageList.append("心月轮(圆)", "/icons/heartmoon_yuan_icon.jpg");




        foreColorfulItem = new ColorfulItem("前景色", "前景色");

        foreColorfulItem.addCommand(foreColorCommand);
        foreColorfulItem.setDefaultCommand(foreColorCommand);
        foreColorfulItem.setItemCommandListener(this);

        bgColorfulItem = new ColorfulItem("背景色", "背景色");

        bgColorfulItem.addCommand(backgroundColorCommand);
        bgColorfulItem.setDefaultCommand(backgroundColorCommand);
        bgColorfulItem.setItemCommandListener(this);

        countKeyColorfulItem = new ColorfulItem("计数键", "");
        countKeyColorfulItem.setKeyTest(true);
        countKeyColorfulItem.addCommand(deleteCountKeyCommand);
        countKeyColorfulItem.setDefaultCommand(deleteCountKeyCommand);
        countKeyColorfulItem.setItemCommandListener(this);

        anykeyCG = new ChoiceGroup("任意键计数", ChoiceGroup.POPUP);
        fullSceenModeCG = new ChoiceGroup("全屏模式", ChoiceGroup.POPUP);
        showImageColorCG = new ChoiceGroup("显示图片", ChoiceGroup.POPUP);
        keepScaleCG = new ChoiceGroup("保持比例", ChoiceGroup.POPUP);

        anykeyCG.append("是", null);
        anykeyCG.append("否", null);

        fullSceenModeCG.append("是", null);
        fullSceenModeCG.append("否", null);

        showImageColorCG.append("是", null);
        showImageColorCG.append("否", null);

        keepScaleCG.append("是", null);
        keepScaleCG.append("否", null);

        boolean[] flags = new boolean[]{false, true};

        anykeyCG.setSelectedFlags(flags);
        fullSceenModeCG.setSelectedFlags(flags);
        showImageColorCG.setSelectedFlags(new boolean[]{true, false});
        keepScaleCG.setSelectedFlags(new boolean[]{true, false});

        append(nameText);
        append(countText);
        append(seqText);
        //append(imagePathText);
        append((Item) (imageList));

        append(foreColorfulItem);
        append(bgColorfulItem);
        append(countKeyColorfulItem);

        append(anykeyCG);
        append(fullSceenModeCG);
        append(showImageColorCG);
        append(keepScaleCG);

    }

    private void saveItem() {
        if (item == null) {
            item = new CounterItem();
        }
        if (anykeyCG.isSelected(0)) {
            item.setAnyKeyCount(true);
        } else {
            item.setAnyKeyCount(false);
        }
        item.setBackColor(bgColorfulItem.getBgColor());
        item.setCounter(getIntValue(countText.getString()));
        item.setForeColor(foreColorfulItem.getForeColor());
        if (fullSceenModeCG.isSelected(0)) {
            item.setFullSceenMode(true);
        } else {
            item.setFullSceenMode(false);
        }

        item.setImagePath(imageList.getImagePath());
        item.setName(nameText.getString());
        if (showImageColorCG.isSelected(0)) {
            item.setShowImage(true);
        } else {
            item.setShowImage(false);
        }

        if (keepScaleCG.isSelected(0)) {
            item.setKeepScale(true);
        } else {
            item.setKeepScale(false);
        }

        item.setSequence(getIntValue(seqText.getString()));
        item.setCountKeys(countKeyColorfulItem.getKeys());

        midlet.addMenuItem(item);
    }

    private int getIntValue(String value) {
        int intVal = -1;
        try {
            intVal = Integer.parseInt(value);
        } catch (Exception ex) {
        }
        return intVal;
    }

    public void setItem(CounterItem item) {
        if (item == null) {
            return;
        }
        this.item = item;
        if (item.isAnyKeyCount()) {
            anykeyCG.setSelectedIndex(0, true);
        }
        if (item.isFullSceenMode()) {
            fullSceenModeCG.setSelectedIndex(0, true);
        }
        if (item.isShowImage()) {
            showImageColorCG.setSelectedIndex(0, true);
        }
        if (item.isKeepScale()) {
            keepScaleCG.setSelectedIndex(0, true);
        }

        nameText.setString(item.getName());
        countText.setString("" + item.getCounter());
        seqText.setString("" + item.getSequence());

        imageList.setImagePath(item.getImagePath());

        foreColorfulItem.setBgColor(item.getBackColor());
        foreColorfulItem.setForeColor(item.getForeColor());
        bgColorfulItem.setBgColor(item.getBackColor());
        bgColorfulItem.setForeColor(item.getForeColor());

        countKeyColorfulItem.setKeys(item.getCountKeys(), ':');

    }

    public void commandAction(Command c, Displayable d) {
        if (c == backCommand) {
            midlet.makeCurrent();
        } else if (c == okCommand) {
            saveItem();
            midlet.makeCurrent();
        } else if (c == okFgCommand) {
            foreColorfulItem.setForeColor(colors.getColor());
            bgColorfulItem.setForeColor(colors.getColor());
            midlet.display.setCurrent(this);
        } else if (c == okBgCommand) {
            foreColorfulItem.setBgColor(colors.getColor());
            bgColorfulItem.setBgColor(colors.getColor());
            midlet.display.setCurrent(this);
        }
    }

    public void commandAction(Command c, Item item) {
        if (c == backgroundColorCommand) {
            if (colors == null) {
                colors = new ColorChooser(midlet.display.isColor());
                colors.setCommandListener(this);
            }

            colors.addCommand(okBgCommand);
            colors.removeCommand(okFgCommand);
            colors.setColor(bgColorfulItem.getBgColor());
            midlet.display.setCurrent(colors);
        } else if (c == foreColorCommand) {
            if (colors == null) {
                colors = new ColorChooser(midlet.display.isColor());
                colors.setCommandListener(this);
            }

            colors.addCommand(okFgCommand);
            colors.removeCommand(okBgCommand);

            colors.setColor(foreColorfulItem.getForeColor());
            midlet.display.setCurrent(colors);
        } else if (c == deleteCountKeyCommand) {
            countKeyColorfulItem.remove();
        }
    }

    /**
     * @return the item
     */
    public CounterItem getItem() {
        return item;
    }
}
